package com.city.ajax.house;

import com.city.domain.house.FamliyDetails;
import com.city.domain.house.UserManage;
import com.city.domain.pub.*;
import com.city.helper.EntityHelper;
import com.city.helper.PubUserJudgeUtils;
import com.city.service.city.CityTownService;
import com.city.service.city.CityVillageService;
import com.city.service.pub.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author: 李顺时
 * @Description: 居民与房屋信息的管理
 * @Date: Create in 14:39 2018/8/12
 * @Modified By:
 * */


@RequestMapping("/ajax/house/userInfo")
@RestController
public class UserInfoAjax {

    @Autowired
    public SaveService saveService;

    @Autowired
    private PubUserService pubUserService;

    @Autowired
    private PubProvinceService provinceService;

    @Autowired
    private PubCityService pubCityService;

    @Autowired
    private PubCountyService pubCountyService;

    @Autowired
    private CityTownService townService;

    @Autowired
    private CityVillageService villageService;

    @Autowired
    private PubNationCategoryService pubNationCategoryService;

    @Autowired
    private PubFamilyRelationshipService relationshipService;

    @Autowired
    private PubIndustryCategoryService industryCategoryService;

    @Autowired
    private PubPoliticalOutlookService outlookService;

    @Autowired
    private PubHouseinfoService pubHouseinfoService;

    @Autowired
    private PubUserJudgeUtils pubUserJudgeUtils;

    @Autowired
    private EntityHelper entityHelper;

    @Autowired
    private PubUserService userService;


    /**
     * @Author: 李顺时
     * @Description: 居民房屋信息管理，查询所有的房屋信息
     * @Date: 15:29 2018/8/12
     */
    @PostMapping("/getPubUser-limit")
    @ResponseBody
    public Map<String,Object> findPubUserinfoLimit(
            @RequestParam(value = "limitPage") Integer limitPage,
            @RequestParam(value ="limitNum") Integer limitNum)
    {
        Map<String,Object> map = new HashMap<String,Object>();

        List<PubUser> user = pubUserService.findAllByLimit2(limitPage,limitNum);
//        List<String> menber = new ArrayList<>();
        List<UserManage> data = new ArrayList<>();
        long startTime = System.currentTimeMillis();
        for (PubUser u: user) {
            UserManage uManage = new UserManage();
//            menber = pubUserService.findFamilyMenberNameByHouseholdId(u.getHouseholdId()); //查询家庭成员。。。 太花时间
            int id = u.getId();
            String workStatus;
            if(("2").equalsIgnoreCase(u.getWork())){
                 workStatus = "创业";
            }else if("1".equalsIgnoreCase(u.getWork())){
                workStatus = "已就业";
            }else {
                workStatus = "待业";
            }
            String sex;
            if(u.getSex() == 1){
                sex = "女";
            }else{
                sex = "男";
            }
            String telphone = u.getTellphone();
            String addr = provinceService.findNameByCode("51")+ pubCityService.findNameByCode("5115")+pubCountyService.findCountyNameByCode("511528")+townService.findTownNameByCode(u.getTownId())+villageService.findVillageNameById(u.getVillageId());
            String name = u.getName();
            String faMenber = "";
//            for (String me: menber) {
//                faMenber += me+"、";
//            }
            uManage.setUserId(id);
            uManage.setName(name);
            uManage.setSex(sex);
            uManage.setAddr(addr);
            uManage.setTelphone(telphone);
//            uManage.setFaMenber(faMenber);
            uManage.setWorkStatus(workStatus);
            data.add(uManage);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间： "+(endTime-startTime)*1.0/1000+"s");
        map.put("data", data);
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count",userService.countfindAllByLimit2());
        return map;
    }


    /**
     * @Author: 李顺时
     * @Description: 模糊查询
     * @param keyword 输入框输入的内容
     * @Date: 19:56 2018/8/12
     */
    @PostMapping("/search-User")
    @ResponseBody
    public Map<String,Object> seachUser(
            @RequestParam(value = "keyword") String keyword,
            @RequestParam(value = "limitPage" ) Integer limitPage,
            @RequestParam(value ="limitNum") Integer limitNum ){
        Map<String,Object> map = new HashMap<String,Object>();
        List<PubUser> user = pubUserService.getUserByNameTelIdcard(keyword,limitPage,limitNum);
        List<UserManage> data = new ArrayList<>();
        if(user!=null){
            for (PubUser u: user) {
                UserManage uManage = new UserManage();
                int id = u.getId();
                String workStatus = pubUserJudgeUtils.getWorkStatus(u.getWork());
                String sex = pubUserJudgeUtils.getSex(u.getSex());

                String telphone = u.getTellphone();
                String addr = provinceService.findNameByCode("51")+ pubCityService.findNameByCode("5115")+pubCountyService.findCountyNameByCode("511500")+townService.findTownNameByCode(u.getTownId())+villageService.findVillageNameById(u.getVillageId());
                String name = u.getName();
                String faMenber = "";
                uManage.setUserId(id);
                uManage.setName(name);
                uManage.setSex(sex);
                uManage.setAddr(addr);
                uManage.setTelphone(telphone);
                uManage.setWorkStatus(workStatus);
                data.add(uManage);
            }
        }
        map.put("data", data);
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count",userService.countBynameTelIdcard(keyword));
        return map;
    }


    /**
     * @Author: 李顺时
     * @Description: 查询同一家庭成员的信息
     * @param id 户主的id
     * @Date: 21:14 2018/8/13
     */
    @PostMapping("/familymenber-info")
    @ResponseBody
    public Map<String,Object> familyMenberInfo(
            @RequestParam("id") Integer id
    ){
        Map<String,Object> map = new HashMap<String,Object>();
        List<PubUser> menber = new ArrayList<>();
        PubUser user = pubUserService.findById(id);
        List<FamliyDetails> menber1 = new ArrayList<>();

        menber = pubUserService.findFamilyMenberByHouseholdId(user.getHouseholdId());

        if(menber!=null){
            for (PubUser m: menber) {
                menber1.add(entityHelper.createFamliyDetails(m));
            }
        }
        map.put("data", menber1);
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count",1000);
        return map;
    }


    /**
     * @Author: 李顺时
     * @Description: 查询同一家庭成员的信息
     * @param id pub_houseinfo表中的id
     * @Date: 20:57 2018/8/23
     */
    @PostMapping("/SeachfamilymenberinfoByHouseId")
    @ResponseBody
    public Map<String,Object> findfamilyMenberInfoByHouseId(
            @RequestParam("id") Integer id
    ){
        Map<String,Object> map = new HashMap<String,Object>();

        PubHouseinfo house = pubHouseinfoService.getHouseinfo(id);
        List<PubUser> user = pubUserService.findFamilyMenberByHouseholdId(house.getId());
        List<FamliyDetails> famliyDetails = new ArrayList<>();


        for (PubUser m: user) {
            FamliyDetails fd = entityHelper.createFamliyDetails(m);
            //将查询出的人员的信息分类 显示成中文。 可能会需要另一个类来接收数据
            famliyDetails.add(fd);
        }
        map.put("data", famliyDetails);
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count",1000);
        return map;
    }


    @PostMapping("/seachByUserId")
    @ResponseBody
    public Map<String,Object> findPubHouseinfoId(@RequestParam(value = "id") Integer id)
    {
        Map<String,Object> map = new HashMap<String,Object>();
        PubUser p = pubUserService.findById(id);
        FamliyDetails famliyDetails = entityHelper.createFamliyDetails(p);

        map.put("data", famliyDetails);
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count",1);
        return map;
    }

    @PostMapping("/seachUserByHouseInfoId")
    @ResponseBody
    public Map<String,Object> findPubHouseinfoByHouseInfoId(@RequestParam(value = "id") Integer id)
    {
        Map<String,Object> map = new HashMap<String,Object>();
        PubHouseinfo houseinfo = pubHouseinfoService.getHouseinfo(id);

        PubUser p = pubUserService.findById(houseinfo.getUserId());
        FamliyDetails famliyDetails = entityHelper.createFamliyDetails(p);

        map.put("data", famliyDetails);
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count",1);
        return map;
    }



    @PostMapping("/seachByUserIdOrigin")
    @ResponseBody
    public Map<String,Object> findPubHouseinfoIdOrigin(@RequestParam(value = "id") Integer id)
    {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("data", pubUserService.findById(id));
        map.put("res", "1");
        map.put("code", "成功");
        map.put("count",1);
        return map;
    }


    @PostMapping("/getNationClass")
    @ResponseBody
    public List<PubNationCategory> getNationClass(){

        return pubNationCategoryService.getAll();
    }

    @PostMapping("/getIndustryClass")
    @ResponseBody
    public List<PubIndustryCategory> getIndustryClass(){

        return industryCategoryService.findAll();
    }

    @PostMapping("/getPoliticalOutlookClass")
    @ResponseBody
    public List<PubPoliticalOutlook> getPoliticalOutlookClass(){

        return outlookService.findAll();
    }

    @PostMapping("/pubuser")
    @Transactional
    @ResponseBody
    public Map<String,String> savePubUser(@RequestBody PubUser pubUser,
                                               HttpServletResponse response,
                                               HttpSession session){
        Map<String,String> map = new HashMap<String, String>();


        PubUser olduser= pubUserService.findById(pubUser.getId());
        pubUser.setHouseholdId(olduser.getHouseholdId());
        pubUser.setEmail(olduser.getEmail());
        pubUser.setRelation(olduser.getRelation());
        pubUser.setQq(olduser.getQq());
        pubUser.setWeixin(olduser.getWeixin());
        pubUser.setBornAddress(olduser.getBornAddress());
        pubUser.setAuthLevel(olduser.getAuthLevel());
        //更新内容

        //保存基本信息
        saveService.save(pubUser);
        map.put("code","信息已录入，请等待回复！");
        return map;
    }



}
